package com.xiaozhichao.databafflesys.common.utils;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.xiaozhichao.databafflesys.common.domain.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;

import java.util.Objects;

/**
 * @author:xiaozhichao
 * @date:2021/3/22
 * 报文比较
 */
public class MessageValid {

    public static boolean valid(String type, String read){
        if(StringUtils.isEmpty(read)){
            return false;
        }
        if(CommonConstant.json.equals(type)){
            return validJSON(read);
        }else if(CommonConstant.xml.equals(type)){
            return validXml(read);
        }else if(CommonConstant.queryString.equals(read)){
            return validJSON(read);
        }else if(CommonConstant.formData.equals(type)){
            return validJSON(read);
        }
        return true;
    }

    public static boolean validJSON(String input){
        Object parseTarget = null;
        try {
            parseTarget = JSON.parse(input);
        } catch (Exception e) {
            return false;
        }
        return Objects.nonNull(parseTarget);
    }

    public static boolean validXml(String input){
        Document document = null;
        try {
            document = XmlUtil.parseXml(input);
        } catch (Exception e) {
            return false;
        }
        return Objects.nonNull(document);
    }

    public static boolean validQueryString(String input){
        return true;
    }

    public static boolean validFormData(String input){
        return true;
    }


}
